// HSS.Logging.DebugHost.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       DebugHost.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
using System;
using System.ServiceModel;
using System.Windows.Forms;

namespace HSS.Logging
{
	/// <summary>
	/// A form to manually host the Logging Service. This is used for development testing only.
	/// </summary>
	public partial class DebugHost : Form
	{
        string startTxt = "Start AutoTruncate Service";
        string stopTxt = "Stop AutoTruncate Service";
		
		/// <summary>
		/// Default constructor
		/// </summary>
		public DebugHost()
		{
			InitializeComponent();
		}

		private void DebugHost_Load(object sender, EventArgs e)
		{
			this.txtURI.Text = Log.HSSLogServerURI + "debug";
			this.txtUser.Text = Environment.UserDomainName + "\\" + Environment.UserName;
        }
		
		/// <summary>
		/// Listens for the showme message
		/// </summary>
		/// <param name="m">A Message</param>
		protected override void WndProc(ref Message m)
		{
			if (m.Msg == NativeMethods.WM_SHOWME)
				ShowMe();
			base.WndProc(ref m);
		}

		private void ShowMe()
		{
			if (WindowState == FormWindowState.Minimized)
				WindowState = FormWindowState.Normal;
			// get our current "TopMost" value (ours will always be false though)
			bool top = TopMost;
			// make our form jump to the top of everything
			TopMost = true;
			// set it back to whatever it was
			TopMost = top;
		}

        void CleanUp()
        {
			if (AutoTruncateService.IsStarted)
				AutoTruncateService.Stop();
        }

		private void btnClose_Click(object sender, EventArgs e)
		{
        	this.Close();
		}

        private void btnAutoTruncate_Click(object sender, EventArgs e)
        {
            if (this.btnAutoTruncate.Text == startTxt)
            {
                AutoTruncateService.Start(this.dtpTruncateAt.Value);
                this.btnAutoTruncate.Text = stopTxt;
            }
            else if (this.btnAutoTruncate.Text == stopTxt)
            {
                AutoTruncateService.Stop();
                this.btnAutoTruncate.Text = startTxt;
            }
        }

        private void DebugHost_FormClosing(object sender, FormClosingEventArgs e)
        {
            CleanUp();
        }

	}
}